package ac.leedsmet.devmanager.guiApp;

import ac.leedsmet.devmanager.DevManagerView;
import ac.leedsmet.devmanager.core.User;

import java.awt.event.ActionEvent;
import java.awt.event.InputMethodEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 * DevManagerView.java
 *
 * The application's main frame.
 * 
 * @author Mike
 * @author Samael
 * @author Takamura
 * @version 1.0
 */
public class CreateUserDialog extends JDialog {

    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;

     /**
     * Class constructor.
     * 
     * @param parent - DevManager application frame
     * @param modal - Indicates modality of dialog
     * 
     * @return CreateUserDialog processing form.
     */
    public CreateUserDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        // Close the dialog when Esc is pressed
        String cancelName = "cancel";
        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
        ActionMap actionMap = getRootPane().getActionMap();
        actionMap.put(cancelName, new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                doClose(RET_CANCEL);
            }
        });
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonPanel = new javax.swing.JPanel();
        CreateUser_okButton = new javax.swing.JButton();
        CreateUser_cancelButton = new javax.swing.JButton();
        InputFieldPanel = new javax.swing.JPanel();
        CreateUsername_Label = new javax.swing.JLabel();
        CreateUsername_TextField = new javax.swing.JTextField();
        CreatePassword_Label = new javax.swing.JLabel();
        CreateFullName_Label = new javax.swing.JLabel();
        CreateFullName_TextField = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ac.leedsmet.devmanager.DevManagerApp.class).getContext().getResourceMap(CreateUserDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        ButtonPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        ButtonPanel.setName("ButtonPanel"); // NOI18N

        CreateUser_okButton.setText(resourceMap.getString("CreateUser_okButton.text")); // NOI18N
        CreateUser_okButton.setName("CreateUser_okButton"); // NOI18N
        CreateUser_okButton.setNextFocusableComponent(CreateUser_cancelButton);
        CreateUser_okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateUser_okButtonActionPerformed(evt);
            }
        });

        CreateUser_cancelButton.setText(resourceMap.getString("CreateUser_cancelButton.text")); // NOI18N
        CreateUser_cancelButton.setName("CreateUser_cancelButton"); // NOI18N
        CreateUser_cancelButton.setNextFocusableComponent(CreateUsername_TextField);
        CreateUser_cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateUser_cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
                ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonPanelLayout.createSequentialGroup()
                                .addContainerGap(159, Short.MAX_VALUE)
                                .addComponent(CreateUser_okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CreateUser_cancelButton)
                                .addContainerGap())
        );

        ButtonPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]{CreateUser_cancelButton, CreateUser_okButton});

        ButtonPanelLayout.setVerticalGroup(
                ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(ButtonPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(CreateUser_cancelButton)
                                        .addComponent(CreateUser_okButton))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getRootPane().setDefaultButton(CreateUser_okButton);

        InputFieldPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        InputFieldPanel.setName("InputFieldPanel"); // NOI18N

        CreateUsername_Label.setText(resourceMap.getString("CreateUsername_Label.text")); // NOI18N
        CreateUsername_Label.setName("CreateUsername_Label"); // NOI18N

        CreateUsername_TextField.setText(resourceMap.getString("CreateUsername_TextField.text")); // NOI18N
        CreateUsername_TextField.setName("CreateUsername_TextField"); // NOI18N
        CreateUsername_TextField.setNextFocusableComponent(PasswordField);
        CreateUsername_TextField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                CreateUsername_TextFieldInputMethodTextChanged(evt);
            }

            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
        });

        CreatePassword_Label.setText(resourceMap.getString("CreatePassword_Label.text")); // NOI18N
        CreatePassword_Label.setName("CreatePassword_Label"); // NOI18N

        CreateFullName_Label.setText(resourceMap.getString("CreateFullName_Label.text")); // NOI18N
        CreateFullName_Label.setName("CreateFullName_Label"); // NOI18N

        CreateFullName_TextField.setText(resourceMap.getString("CreateFullName_TextField.text")); // NOI18N
        CreateFullName_TextField.setName("CreateFullName_TextField"); // NOI18N
        CreateFullName_TextField.setNextFocusableComponent(CreateUser_okButton);
        CreateFullName_TextField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                CreateFullName_TextFieldInputMethodTextChanged(evt);
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
        });

        PasswordField.setText(resourceMap.getString("PasswordField.text")); // NOI18N
        PasswordField.setName("PasswordField"); // NOI18N
        PasswordField.setNextFocusableComponent(CreateFullName_TextField);
        PasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasswordFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout InputFieldPanelLayout = new javax.swing.GroupLayout(InputFieldPanel);
        InputFieldPanel.setLayout(InputFieldPanelLayout);
        InputFieldPanelLayout.setHorizontalGroup(
            InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, InputFieldPanelLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(CreateFullName_Label)
                    .addComponent(CreatePassword_Label)
                    .addComponent(CreateUsername_Label))
                .addGap(18, 18, 18)
                .addGroup(InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(CreateFullName_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(CreateUsername_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
        InputFieldPanelLayout.setVerticalGroup(
            InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InputFieldPanelLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(CreateUsername_Label)
                    .addComponent(CreateUsername_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CreatePassword_Label)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(InputFieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(CreateFullName_Label)
                    .addComponent(CreateFullName_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(InputFieldPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(InputFieldPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }

    private void CreateUser_okButtonActionPerformed(ActionEvent evt) {
        User user = DevManagerView.getUserManager().createUser(CreateUsername_TextField.getText(), PasswordField.getText());
        user.setFullname(CreateFullName_TextField.getText());
        doClose(RET_OK);
    }
    
    private void CreateUser_cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        doClose(RET_CANCEL);
    }

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {
        doClose(RET_CANCEL);
    }

    private void CreateUsername_TextFieldInputMethodTextChanged(InputMethodEvent evt) {
        // TODO add your handling code here:
        //    ????????.setTitle(CreateUsername_TextField.getText());
    }

    private void CreateFullName_TextFieldInputMethodTextChanged(InputMethodEvent evt) {
        // TODO add your handling code here:
        //    ????????.setFullName(CreateFullName_TextField.getText());
    }

    private void PasswordFieldActionPerformed(ActionEvent evt) {
        // TODO add your handling code here:
        // Put code to validate password here
    }

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CreateUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CreateUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CreateUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CreateUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CreateUserDialog dialog = new CreateUserDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JLabel CreateFullName_Label;
    private javax.swing.JTextField CreateFullName_TextField;
    private javax.swing.JLabel CreatePassword_Label;
    private javax.swing.JButton CreateUser_cancelButton;
    private javax.swing.JButton CreateUser_okButton;
    private javax.swing.JLabel CreateUsername_Label;
    private javax.swing.JTextField CreateUsername_TextField;
    private javax.swing.JPanel InputFieldPanel;
    private javax.swing.JPasswordField PasswordField;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;
}
